home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 317 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  108 lines

  1. Newsgroups: alt.msdos.programmer,comp.lang.c
  2. Path: news.kei.com!wang!news
  3. From: emild@cs.technion.ac.il (Kohn Emil Dan)
  4. Subject: Re: Pascal program works but not C program! 
  5. Organization: Technion, Israel Institute of Technology
  6. Date: Thu, 4 Jan 1996 10:18:15 GMT
  7. Message-ID: <Pine.SV4.3.91-heb-2.04.960104114930.16292A-100000@cs.technion.ac.il>
  8. References: <4cdpr2$psi@lugb.latrobe.edu.au> 
  9. Sender: news@wang.com
  10.  
  11.  
  12.  
  13. On 3 Jan 1996, Gregary John Boyles wrote:
  14.  
  15. > EMail : boyles@lux.latrobe.edu.au
  16. >       
  17. > Why does the Pascal version of this program behave exactly as expected while the
  18. > C++ version does not?
  19. > ******************************* PASCAL VERSION *******************************
  20. /*Pascal  code removed*/ 
  21. > ******************************** C++ VERSION *********************************
  22. > #include <stdio.h>
  23. > #include <conio.h>
  24. > #include <string.h>
  25. > #include <alloc.h>
  26. > #include <process.h>
  27. > const maxstring=127;
  28. > const lineend='\n';
  29. > const escape=27;
  30. > const null='\0';
  31. > const space=' ';
  32. > const up='H';
  33. > const down='P';
  34. > const pageup='I';
  35. > const pagedown='Q';
  36. > const left='K';
  37. > const right='M';
  38. > const home='G';
  39. > const end_='O';
  40. > void writescreenfull(char width,char height,nodetypeptr posptr,char stringpos)
  41. > {
  42. >    char y;
  43. >    window(1,1,width,height);
  44. >    clrscr();
  45. >    window(1,1,width,height+1);
  46. >    y=1;
  47. >    while ((y<=height) && (!empty(posptr->next)))
  48. >    {
  49. >         gotoxy(1,y);
  50. >         strcpy(line_,posptr->line);
  51. >         line__=&(line_[stringpos]);
  52. >         strncpy(line,line__,width-1);
  53. >         strcat(line,null);
  54.                        ^---------------This is a problem!!!!!!!!!
  55.                        
  56. >         cputs(line);
  57. >         posptr=posptr->next;
  58. >         y++;
  59. >    }
  60. > }
  61. > void main(int argc,char *argv[])
  62.    ^-----------------------------This one too, but not too serious
  63. > {
  64. > }
  65.  
  66.  
  67. I hadn't read  your code thoroughly, but I think your problem is with 
  68. strcat (I mean surely there is a problem with the usage of strcat, but I 
  69. don't know if it's the only one). In C (and in C++ too), a string of length 
  70. 1 is _NOT_ the same thing as a character. So null (as defined by you) is _NOT_ a string.
  71. An empty string is defined as "". If you want to append a '\0' string 
  72. terminator to line_  (I think this was your intention), use the statement:
  73.  
  74. line_[width-1]='\0'; (or using your constant line_[width-1]=null;) 
  75.  
  76. Don't bother, if the length of line__ is less than width-1; strncpy padds 
  77. the destination string with '\0'-s.
  78.  
  79.  
  80.  
  81. Another problem, not as serious as the previous one, although this is not
  82. the reason the C++ version might not work; main() should be defined
  83. returning int, not void. 
  84.  
  85.  
  86. Hope this helps.
  87.  
  88.  
  89.                     Best regards, 
  90.  
  91.                                 Emil
  92.